package com.jike.mahjong.controller;


import com.jike.mahjong.common.Page;
import com.jike.mahjong.common.util.HttpUtil;
import com.jike.mahjong.model.Manager;
import com.jike.mahjong.model.MessageModel;
import com.jike.mahjong.model.Player;
import com.jike.mahjong.model.Version;
import com.jike.mahjong.service.MessageService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 游戏消息，公告等内容
 * Created by star on 2016/10/12.
 */
@Controller
public class MessageController {

    private Logger log = Logger.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

    /**
     * 查询
     *
     * @param request
     * @param response
     * @param message
     * @param model
     * @return
     * @throws Exception
     *//*

    @RequestMapping(value = "/message/queryMessageList.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String queryPlayerList(HttpServletRequest request, HttpServletResponse response, MessageModel message, Model model) throws Exception {
       *//* Page page=new Page();
        Map<String, Object> para = new HashMap<String, Object>();
        String messageTitle = HttpUtil.getHttpParameter(request, "messageTitle");
        String currentPage = HttpUtil.getHttpParameter(request, "pageIndex");

        if(!"".equals(currentPage)&&currentPage!=null){
            page.setCurrentPage(Integer.parseInt(currentPage));

        }else{
            page.setCurrentPage(1);
        }
        para.put("messageTitle", messageTitle);
        page = messageService.queryMessageInfo(para,page);
        model.addAttribute("messageTitle", messageTitle);
        model.addAttribute("page", page);*//*
        return "views/title/messageShow";
    }*/

    /**
     * 添加
     *
     * @param request
     * @param response
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/message/addMessage.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String addMessage(HttpServletRequest request, HttpServletResponse response, Model model,MessageModel message) throws Exception {
        Map<String, Object> para = new HashMap<String, Object>();
        Manager manager = (Manager) request.getSession().getAttribute("loginUser");
//        MessageModel message = new MessageModel();

        int effectRow = messageService.insertMessage(message);
        model.addAttribute("effectRow", effectRow);
        Page page=new Page();
        String currentPage = HttpUtil.getHttpParameter(request, "pageIndex");

        if(!"".equals(currentPage)&&currentPage!=null){
            page.setCurrentPage(Integer.parseInt(currentPage));

        }else{
            page.setCurrentPage(1);
        }

        model.addAttribute("page",queryAllMessage(para,page) );
        return "views/title/messageShow";
    }



    /**
     * 添加任务
     *
     * @param request
     * @param response
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/message/addMsg.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String addSendMessage(HttpServletRequest request, HttpServletResponse response, Model model,MessageModel message) throws Exception {
        Map<String, Object> para = new HashMap<String, Object>();
        String conten=HttpUtil.getHttpParameter(request, "msg");
        String frequency=HttpUtil.getHttpParameter(request, "frequency");

        messageService.sendMsg(conten,Integer.valueOf(frequency));

        model.addAttribute("conten",conten);
        model.addAttribute("frequency",frequency);
        return "views/title/messageShow";
    }

    @RequestMapping(value = "/message/stopMsg.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String stopMsg(HttpServletRequest request, HttpServletResponse response, Model model,MessageModel message) throws Exception {
        Map<String, Object> para = new HashMap<String, Object>();
        String conten=HttpUtil.getHttpParameter(request, "msg");
        String frequency=HttpUtil.getHttpParameter(request, "frequency");
        messageService.stopMsg();
        model.addAttribute("conten",conten);
        model.addAttribute("frequency",frequency);
        return "views/title/messageShow";
    }


    @RequestMapping(value = "/message/deleteMessage.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String deleteMessage(HttpServletRequest request, HttpServletResponse response, Model model) {
        Page page=new Page();
        Map<String, Object> para = new HashMap<String, Object>();
        para.put("id", HttpUtil.getHttpParameter(request, "id"));
        int effectRow = messageService.deleteMessage(para);
        model.addAttribute("effectRow", effectRow);

        String currentPage = HttpUtil.getHttpParameter(request, "pageIndex");

        if(!"".equals(currentPage)&&currentPage!=null){
            page.setCurrentPage(Integer.parseInt(currentPage));

        }else{
            page.setCurrentPage(1);
        }

        model.addAttribute("page",queryAllMessage(para,page) );
        return "views/title/messageShow";
    }


    public Page queryAllMessage(Map<String, Object> para,Page page) {
        page = messageService.queryMessageInfo(para,page);
        return page;

    }


    @RequestMapping(value = "/version/queryVersionInfo.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String queryVersion(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        Map<String, Object> para = new HashMap<String, Object>();

        model.addAttribute("vo",messageService.queryVersionInfo(para));

        return "views/title/versionControl";
    }


    @RequestMapping(value = "/version/updateVersion.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String updateVersion(HttpServletRequest request, HttpServletResponse response, Version vo, Model model) throws Exception {

        messageService.updateVersionInfo(vo);
        Map<String, Object> para = new HashMap<String, Object>();
        model.addAttribute("vo",messageService.queryVersionInfo(para));
        return "views/title/versionControl";
    }




}
